package com.woniuxy.house.service;

import com.woniuxy.house.entity.ApiSecurityUser;
import com.woniuxy.house.entity.User;
import com.woniuxy.house.exception.AccountExistException;
import com.woniuxy.house.exception.AccountNotExistException;
import com.woniuxy.house.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 点击登录后security查询用户数据
 * @author baobo
 * @date 2022-08-29-11:37
 */
@Service
public class MyUserDetailService implements UserDetailsService {
	@Autowired
	private UserMapper userMapper;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		//通过账号查找用户
		User user=userMapper.getByAccount(username);
		if(user==null){
			throw new AccountNotExistException("用户不存在");
		}
		List<GrantedAuthority> authorities=new ArrayList<>();

		return new ApiSecurityUser(user.getId(),user.getAccount(),user.getPassword(),authorities);
	}
}
